﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;
using Microsoft.Practices.ServiceLocation;

namespace DynamicUIFramework
{
    /// <summary>
    /// PropertyDialog.xaml 的交互逻辑
    /// </summary>
    public partial class PropertyDialog : Window
    {
        public PropertyDialog()
        {
            InitializeComponent();
        }

        public IElement Target { get; set; }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            ContentControl con = Target as ContentControl;
            if (con != null)
            {
                string content = this.txtContent.Text.Trim();
                if(con.Content == null || con.Content.GetType().Equals(typeof(string)))
                {
                    con.Content = content;
                }
            }

            Target.ReferenceResourceKey = this.cbStyle.Text.Trim();
            FrameworkElement fe = (Target as FrameworkElement);
            if (string.IsNullOrEmpty(Target.ReferenceResourceKey))
                fe.SetResourceReference(FrameworkElement.StyleProperty, Target.GetType());
            else
                fe.SetResourceReference(FrameworkElement.StyleProperty, Target.ReferenceResourceKey);
            ICommandSource cs = this.Target as ICommandSource;
            if (cs != null)
            {
                string commandName = this.cbCommand.Text.Trim();
                string commandParameter = this.txtParameter.Text.Trim();

                if (string.IsNullOrEmpty(commandName))
                {
                    this.Target.GetType().GetProperty("Command").SetValue(this.Target, null, null);
                    this.Target.GetType().GetProperty("CommandParameter").SetValue(this.Target, null, null);
                }
                else
                {
                    CommandController cc = ServiceLocator.Current.GetInstance<CommandController>();
                    this.Target.GetType().GetProperty("Command").SetValue(this.Target, cc.Commands[commandName], null);
                    this.Target.GetType().GetProperty("CommandParameter").SetValue(this.Target, string.IsNullOrEmpty(commandParameter) ? null : commandParameter, null);
                }
            }
            
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement fe = (Target as FrameworkElement);
            if (string.IsNullOrEmpty(Target.ReferenceResourceKey))
                fe.SetResourceReference(FrameworkElement.StyleProperty, Target.GetType());
            else
                fe.SetResourceReference(FrameworkElement.StyleProperty, Target.ReferenceResourceKey);

            this.Close();
        }

        public List<string> GetThemeKey(Type type)
        {
            List<string> result = new List<string>();
            DoFind(type, Application.Current.Resources, result);
            //if (result.Contains(string.Empty) == false)
            //    result.Insert(0, string.Empty);
            return result;
        }

        private void DoFind(Type type, ResourceDictionary rd, List<string> result)
        {
            foreach (var item in rd.MergedDictionaries)
            {
                foreach (DictionaryEntry res in item)
                {
                    Style s = res.Value as Style;
                    if (s == null)
                        continue;

                    if (s.TargetType.FullName.Equals(type.FullName))
                    {
                        if (res.Key is string)
                        {
                            if (result.Contains(res.Key.ToString()) == false)
                                result.Add(res.Key.ToString());
                        }
                    }
                }
                if (item.MergedDictionaries.Count > 0)
                    DoFind(type, item, result);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ContentControl con = Target as ContentControl;
            if(con != null)
            {
                if(con.Content!= null && con.Content.GetType().Equals(typeof(string)))
                {
                    this.txtContent.Text = con.Content.ToString();
                }
                else
                {
                    this.txtContent.Text = string.Empty;
                }
            }

            this.cbStyle.Items.Clear();
            List<string> syles = GetThemeKey(Target.GetType());
            foreach (var item in syles)
            {
                this.cbStyle.Items.Add(item);
            }
            this.cbStyle.Items.Insert(0, string.Empty);
            this.cbStyle.Text = string.IsNullOrEmpty(Target.ReferenceResourceKey) ? string.Empty : Target.ReferenceResourceKey; 

            CommandController cc = ServiceLocator.Current.GetInstance<CommandController>();
            this.cbCommand.Items.Clear();
            foreach (var item in cc.Commands)
            {
                this.cbCommand.Items.Add(item.Key);
            }
            this.cbCommand.Items.Insert(0, string.Empty);

            ICommandSource cs = this.Target as ICommandSource;
            if (cs != null)
            {
                this.cbCommand.Text = cc.GetCommandName(cs.Command);
                this.txtParameter.Text = cs.CommandParameter == null ? string.Empty : cs.CommandParameter.ToString();
            }
        }

        private void cbStyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FrameworkElement fe = (Target as FrameworkElement);
            if (string.IsNullOrEmpty(this.cbStyle.SelectedItem.ToString()))
                fe.SetResourceReference(FrameworkElement.StyleProperty, Target.GetType());
            else
                fe.SetResourceReference(FrameworkElement.StyleProperty, this.cbStyle.SelectedItem.ToString().Trim());
        }


    }
}
